﻿using FakeXiecheng.API.Database;
using FakeXiecheng.API.Dto;
using FakeXiecheng.API.Dtos;
using FakeXiecheng.API.Models;
using FakeXiecheng.API.Services;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;

namespace FakeXiecheng.API.Services
{
    public class TouristRouteRepository : ITouristRouteRepository
    {

        private readonly AppDbContext _appDbContext;

        public TouristRouteRepository(AppDbContext appDbContext)
        {
            _appDbContext = appDbContext;
        }


        /// <summary>
        /// 根据ID查询路径
        /// </summary>
        /// <param name="touristRouteId"></param>
        /// <returns></returns>
        public async Task<TouristRoute> GetTouristRouteAsync(Guid touristRouteId)
        {
            return await _appDbContext.TouristRoutes
                .Include(t => t.TouristRoutePictures)
                .FirstOrDefaultAsync(n => n.Id == touristRouteId);
        }

        /// <summary>
        /// 查询所有路线数据
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<TouristRoute>> GetTouristRoutesAsync(string keyword, string ratingOperator, int? ratingValue)
        {

            IQueryable<TouristRoute> result = _appDbContext
                .TouristRoutes
                .Include(t => t.TouristRoutePictures);
            if (!string.IsNullOrEmpty(keyword))
            {
                keyword = keyword.Trim();
                result = result.Where(t => t.Title.Contains(keyword));//构建sql语句，还没有执行IQueryable
            }
            if (ratingValue >= 0)
            {
                result = ratingOperator switch
                {
                    "largerThan" => result.Where(t => t.Rating >= ratingValue),
                    "lessThan" => result.Where(t => t.Rating <= ratingValue),
                    _ => result.Where(t => t.Rating == ratingValue),
                };
            }

            return result.ToList(); //执行IQueryable查询数据库
        }

        /// <summary>
        /// 判断旅游路线是否存在
        /// </summary>
        /// <param name="touristRouteId"></param>
        /// <returns></returns>
        public async Task<bool> TouristRoutesExistAsync(Guid touristRouteId)
        {
            return await _appDbContext.TouristRoutes.AnyAsync(t => t.Id == touristRouteId);
        }

        /// <summary>
        /// 查询Picture数据库，获取信息
        /// </summary>
        /// <param name="touristRouteId"></param>
        /// <returns></returns>
        public async Task<IEnumerable<TouristRoutePicture>> GetTouristRoutePictureByTouristRouteIdAsync(Guid touristRouteId)
        {
            return await _appDbContext.TouristRoutePictures.Where(t => t.TouristRouteId == touristRouteId).ToListAsync();
        }

        /// <summary>
        /// 根据id查询单个图片
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<TouristRoutePicture> GetTouristRoutePictureAsync(int Id)
        {
            return await _appDbContext.TouristRoutePictures.Where(p => p.Id == Id).FirstOrDefaultAsync();
        }

        /// <summary>
        /// 添加路线
        /// </summary>
        /// <param name="touristRoute"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public void AddTouristRoute(TouristRoute touristRoute)
        {
            if (touristRoute == null)
            {
                throw new ArgumentNullException(nameof(touristRoute));
            }
            _appDbContext.TouristRoutes.Add(touristRoute);//写入数据库上下文（在内存中保存，还没有插入数据库）

        }

        /// <summary>
        /// 添加路线图片
        /// </summary>
        /// <param name="touristRouteId"></param>
        /// <param name="picture"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public void AddTouristRoutePicture(Guid touristRouteId, TouristRoutePicture picture)
        {
            if (touristRouteId == null)
            {
                throw new ArgumentNullException(nameof(touristRouteId));
            }
            if (picture == null)
            {
                throw new ArgumentNullException(nameof(picture));
            }

            picture.TouristRouteId = touristRouteId;
            _appDbContext.TouristRoutePictures.Add(picture);

        }

        /// <summary>
        /// 删除单一资源
        /// </summary>
        /// <param name="touristRoute"></param>
        public void DeleteTouristRoute(TouristRoute touristRoute)
        {
            _appDbContext.TouristRoutes.Remove(touristRoute);
        }


        /// <summary>
        /// 删除路线子资源图片单一资源
        /// </summary>
        /// <param name="touristRoutePicture"></param>
        public void DeleteTouristRoutePicture(TouristRoutePicture touristRoutePicture)
        {
            _appDbContext.TouristRoutePictures.Remove(touristRoutePicture);
        }

        /// <summary>
        /// 批量查詢资源
        /// TODO 批量获取资源接口 待测试
        /// </summary>
        /// <param name="touristRouteIds"></param>
        /// <returns></returns>
        public async Task<IEnumerable<TouristRoute>> GetBatchTouristRouteAsync(IEnumerable<Guid> touristRouteIds)
        {
            return await _appDbContext.TouristRoutes.Where(t => touristRouteIds.Contains(t.Id)).ToListAsync();
        }

        /// <summary>
        /// 批量删除
        /// TODO 批量删除接口待测试
        /// </summary>
        /// <param name="touristRoutes"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void BatchDeleteTouristRoute(IEnumerable<TouristRoute> touristRoutes)
        {
            _appDbContext.TouristRoutes.RemoveRange(touristRoutes);
        }

        #region 数据库插入方法
        /// <summary>
        /// 数据库的插入方法
        /// </summary>
        /// <returns></returns>
        public async Task<bool> SaveAsync()
        {
            return (await _appDbContext.SaveChangesAsync() >= 0);//向数据库插入数据
        }


        #endregion
    }
}
